
//自适应模块高度
$(window).ready(
		function() { // 当浏览器变化时
			var adImgWidth = $(".advert-mode .img").width();
			var adImgHeight = adImgWidth / 800 * 450;
			$(".advert-mode .img").height(adImgHeight);
			$(".advert-mode .img span").css("lineHeight", adImgHeight + "px");
			$(".advert-mode .img .control").css("margin-top",
					"-" + adImgHeight + "px");
		}
		);
$(window).resize(
		function() { // 当浏览器变化时
			var adImgWidth = $(".advert-mode .img").width();
			var adImgHeight = adImgWidth / 800 * 450;
			$(".advert-mode .img").height(adImgHeight);
			$(".advert-mode .img span").css("lineHeight", adImgHeight + "px");
			$(".advert-mode .img .control").css("margin-top",
					"-" + adImgHeight + "px");
		}
		);

//导入数据
loadListData();
function loadListData() {
	
	var formObject={"deptId":1}
	
	$.ajax({
		type:"POST",
		url:DataURL+"nybank/promote/findPromoteList",
		dataType:'json', 
		async : false,
		data:{"deptId":1},//传递给服务器的参数
		success:function(data){
			if(checkTokenError(data)){
			//console.log(data.content.length);
			EntryTable(data.content);}
			
		},
        error:function(data){
            console.log(data)
            zxTips_short({
    			txt : "系统错误"
    			});
        }
	});
}

function EntryTable(TableData) {
	TableData.sort(DataSortByDate);
	var TableList = "";
	TableDataLength = TableData.length;  
	for (var i = 0; i < TableData.length; i++) {
		TableList += '<div class="advert-mode" data-id="'+TableData[i].id+'">';
		TableList += '<div class="title">'+TableData[i].title+'</div>';
		TableList += '<div class="img">';
		TableList += '<img alt="广告位" src="' + TableData[i].image+'">';
		TableList += '<div class="control">';
		TableList += '<span class="control-button delete">删除</span>';
		TableList += '</div>';
		TableList += '</div>';
		TableList += '</div>';
	}
	if (TableDataLength < 6) {
		TableList += '<div class="advert-mode">';
		TableList += '<div class="title">添加广告位</div>';
		TableList += '<div class="img add">';
		TableList += '<span>+</span>';
		TableList += '</div>';
		TableList += '</div>';
	}
	$('.advert-list').html(TableList);
}
//根据时间排序
function DataSortByDate(x, y) {
    return (x.createDate < y.createDate) ? 1 : -1
    		 
}
//删除数据
$('.control-button.delete').click( function () {
	var _this=$(this);
	zxTips_classic.call(_this,{
		Head : false,
		// 是否存在头部
		ContentText : "是否<span class='statu2'>删除</span>这个广告位？",
		// 提示内容
		Foot : true,
		// 是否拥有底部
		FootCancel : true,
		// 是否拥有底部取消按钮
		FootEnter : true,
		// 是否拥有底部确认按钮
		clickEnter : function() {
			var formData={"id":_this.parents(".advert-mode").attr("data-id")}
			var CheckSuccess=true;
			console.log(formData);
			
			$.ajax({
				type:"POST",
				url:DataURL+'nybank/promote/delPromote',
				data: formData,
				dataType:'json', 
				async : false,
				success:function(data){
					if(checkTokenError(data)){
					CheckAddMode();
					zxTips_short({txt:"删除成功"});
					zxTips_classicClose();
					}
				},
		        error:function(data){
		            console.log(data)
		            zxTips_short({
		    			txt : "系统错误"
		    			});
		            zxTips_classicClose();
		        }
			});
			_this.parents(".advert-mode").remove();

		}
	});
} );
//检查是否添加新增模块
function CheckAddMode(){
	if(TableDataLength>5){
		var TableList = "";
		TableList += '<div class="advert-mode">';
		TableList += '<div class="title">添加广告位</div>';
		TableList += '<div class="img add">';
		TableList += '<span>+</span>';
		TableList += '</div>';
		TableList += '</div>';
		$('.advert-list').append(TableList);
		initAddMode();
	}
	TableDataLength--;
}
function initAddMode(){
	var adImgWidth = $(".advert-mode .img").width();
	var adImgHeight = adImgWidth / 800 * 450;
	$(".advert-mode .img").height(adImgHeight);
	$(".advert-mode .img span").css("lineHeight", adImgHeight + "px");
	$(".advert-mode .img .control").css("margin-top","-" + adImgHeight + "px");
	$(".advert-mode .img.add").unbind();
	$('.advert-mode .img.add').click( function () {
		window.location.href = HostURL	+ '/activityManager/newAdvert';
	} );
}

//跳转到添加页
$('.advert-mode .img.add').click( function () {
	window.location.href = HostURL	+ '/activityManager/newAdvert';
} );
